package com.example.passport.mapper;

import com.example.passport.entity.Token;
import org.apache.ibatis.annotations.*;

import java.util.Date;

@Mapper
public interface TokenMapper {

    @Select("SELECT * FROM token WHERE token = #{token} LIMIT 1")
    Token findByToken(@Param("token") String token);

    @Delete("DELETE FROM token WHERE created_at <= #{before}")
    int deleteInvalid(@Param("before") Date before);

    @Insert("INSERT INTO token (user_id, token) VALUES (#{userId}, #{token})")
    void create(@Param("userId") long userId, @Param("token") String token);

    @Delete("DELETE FROM token WHERE token = #{token} AND user_id = #{userId}")
    int delete(@Param("token") String token, @Param("userId") long userId);

}
